import unittest
from datetime import datetime

from django.test import TestCase
from backend.django_res.orm.models import SupplierModel, BuyOrderModel


# Create your tests here.
class SupplierTestCast(TestCase):
    def test_use(self):
        expect_name = 'JD'
        expect_cno = '13812348765'
        expect_cname = 'liuqiangdong'
        supplier = SupplierModel.objects.create(name=expect_name,
                                                contact_no=expect_cno,
                                                contact_name=expect_cname)
        self.assertEqual(expect_cname, supplier.contact_name)

        supplier.contact_name = 'Daqiangzi'
        supplier.save()

        supplier_up = SupplierModel.objects.get(id=supplier.id)
        self.assertEqual('Daqiangzi', supplier_up.contact_name)


class BuyOrderTestCast(TestCase):
    def test_use(self):
        expect_name = 'JD'
        expect_cno = '13812348765'
        expect_cname = 'liuqiangdong'
        supplier = SupplierModel.objects.create(name=expect_name,
                                                contact_no=expect_cno,
                                                contact_name=expect_cname)

        order1 = BuyOrderModel.objects.create(supplier=supplier,
                                              buy_date_time=datetime.now(),
                                              digest='something',
                                              amount=1,
                                              price='100.13',
                                              unit='yuan')

        orders = supplier.buy_orders.all()
        self.assertEqual(1, len(orders))
        self.assertEqual(order1, orders[0])

        order2 = BuyOrderModel.objects.create(supplier=supplier,
                                              buy_date_time=datetime.now(),
                                              digest='food',
                                              amount=5,
                                              price='68.99',
                                              unit='yuan')
        orders = supplier.buy_orders.all()
        self.assertEqual(2, len(orders))
        self.assertTrue(order2 in orders)

        SupplierModel.objects.create(name='pdd',
                                     contact_no='18612340000',
                                     contact_name='Tom')
        tb = SupplierModel.objects.create(name='TaoBao',
                                          contact_no='13912344321',
                                          contact_name='Jack Ma')

        BuyOrderModel.objects.create(supplier=tb,
                                     buy_date_time=datetime.now(),
                                     digest='eat food',
                                     amount=5,
                                     price='68.99',
                                     unit='yuan')

        self.assertEqual(3, SupplierModel.objects.count())

        food_suppliers = SupplierModel.objects.filter(buy_orders__digest__contains='food')
        self.assertEqual(2, len(food_suppliers))
        print(food_suppliers.db)


if __name__ == '__main__':
    unittest.main(verbosity=2)
